package QQServe;

import Common.Message;
import Common.MessageType;

import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.util.HashMap;
import java.util.Set;

public class ServeConnectClientThread extends Thread{
    private Socket socket;
    private String userId;

    public Socket getSocket() {
        return socket;
    }

    public ServeConnectClientThread(Socket socket, String userId){
        this.socket=socket;
        this.userId=userId;
    }

    @Override
    public void run() {
        while (true){
            try {
                System.out.println("服务端和客户端"+userId+"连接");
                //获取流对象 读取客户端回写的数据
                ObjectInputStream ois=new ObjectInputStream(socket.getInputStream());
                //获得对象数据 强转
                Message message= (Message) ois.readObject();
                //下拉列表的业务逻辑
                //通过进程 获取在线人数
                if(message.getMsgType().equals(MessageType.MESSAGE_ONLINE_FRIENDS)){
                    System.out.println("获取用户列表");
                    HashMap<String,ServeConnectClientThread> hm=ManageClientThread.getHm();
                    Set<String> strings = hm.keySet();
                    String onlineFriend="";
                    for(String string:strings){
                        onlineFriend+=strings+"";
                    }
                    Message m2=new Message();
                    m2.setMsgType(MessageType.MESSAGE_ONLINE_FRIENDS);
                    m2.setContent(onlineFriend);
                    m2.setSender(message.getSender());
                    //回写
                    ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
                    oos.writeObject(m2);

                }else if (message.getMsgType().equals(MessageType.MESSAGE_CLINT_EXIT)){
                    System.out.println(message.getSender()+"退出");
                    ManageClientThread.removeThread(message.getSender());
                    socket.close();
                    //结束线程
                }else if (message.getMsgType().equals(MessageType.MESSAGE_COMMON)){
                    //私聊
                    String getterId = message.getReceiver();
                    //拿到线程
                    ServeConnectClientThread thread = ManageClientThread.getThread(getterId);
                    Socket socket1 = thread.getSocket();
                    new ObjectOutputStream(socket1.getOutputStream()).writeObject(message);

                }else if (message.getMsgType().equals(MessageType.MESSAGE_COMMON_ALL)){
                    //群聊
                    //拿到所有线程
                     HashMap<String,ServeConnectClientThread> hm =ManageClientThread.getHm();
                     Set<String> userId = hm.keySet();
                    for (String id : userId) {
                        ServeConnectClientThread thread = ManageClientThread.getThread(id);
                        Socket socket1 = thread.getSocket();
                        new ObjectOutputStream(socket1.getOutputStream()).writeObject(message);
                    }
                }else if (message.getMsgType().equals(MessageType.MESSAGE_FILE_SEND)){
                    System.out.println(ManageClientThread.getThread(message.getReceiver()));
                    ObjectOutputStream stream = new ObjectOutputStream(ManageClientThread.getThread(message.getReceiver()).getSocket().getOutputStream());
                    stream.writeObject(message);
                }

            }catch (Exception e){
                e.getMessage();
            }
        }
    }


}
